﻿<cfscript>
/** 
* @hint "acadmicBase - Building"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.buildingListener" 
	output=false 
	depends="sequenceBuilding"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	/**
	* @hint "删除建筑物信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteBuilding( required MachII.framework.Event event ) output=false {
		
		var campus = "";
		var build = "";
		var passArg = structNew();

		/* Every SQL operation is TRANSACATION*/
		transaction {

			var sql = "	SELECT
							b.b_id,
							b.campus_id
						FROM
							t_building b
						WHERE
							b.b_id = :bid ";

			var queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "bid", value = event.getArg("BuildingID"), cfsqltype = "cf_sql_char");
			var rs_buildEntity = queryObj.execute(sql = sql).getResult();

			var buildingId = rs_buildEntity.b_id;
			var campusId = rs_buildEntity.campus_id;

			sql = "	DELETE FROM
						t_building b
					WHERE
						b.b_id = :bid ";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "bid", value = event.getArg("BuildingID"), cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);

			event.setArg("CID", campusId);
			event.setArg("BID", buildingId);
			event.setArg("TabID", "labBuilding");
			event.setArg("Notice", "buildRemoved");
		}
		
	}
	
	/**
	* @hint "更新建筑物信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateBuilding( required MachII.framework.Event event ) output=false {
		var campus = "";
		var build = "";
		var passArg = structNew();
		
		/* Every SQL operation is TRANSACATION*/
		transaction {

			var sql = "	UPDATE
							t_building b
						SET
							b.b_name = :buildingName ,
							b.b_floors = :floors ,
							b.campus_id = :campusId 
						WHERE
							b.b_id = :buildingId ";
			
			var queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "buildingName", value = event.getArg("BuildingName"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "floors", value = parseFloor(event.getArg("FloorsUp"), event.getArg("FloorsDown")), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "campusId", value = event.getArg("CampusID"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "buildingId", value = event.getArg("BuildingID"), cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);

			event.setArg("CID", event.getArg("CampusID"));
			event.setArg("BID", event.getArg("BuildingID"));
			event.setArg("TabID", "labBaseInfo");
			event.setArg("Notice", "buildUpdated");
			
		}
		
	}
	
	/**
	* @hint "创建建筑物信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createBuilding( required MachII.framework.Event event ) output=false {
		var sequenceBuilding = getsequenceBuilding();
		var campus = "";
		var build = "";
		var passArg = structNew();

		/* Every SQL operation is TRANSACATION*/
		transaction {

			sequenceBuilding.setCampusID(event.getArg("CampusID"));
			var buildingId = sequenceBuilding.getID();
			
			var sql = "	INSERT INTO t_building
						(
							b_id,
							b_name,
							b_floors,
							campus_id
						)VALUES(
							:buildingId ,
							:buildingName ,
							:floors ,
							:campusId 
						)";
			
			var queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "campusId", value = event.getArg("CampusID"), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "floors", value = parseFloor(event.getArg("FloorsUp"), event.getArg("FloorsDown")), cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "buildingName", value = event.getArg("BuildingName"), cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "buildingId", value = buildingId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			event.setArg("CID", event.getArg("CampusID"));
			event.setArg("BID", buildingId);
			event.setArg("TabID", "labBuilding");
			
		}
	}
	
	/**
	* @hint "根据地上和地下楼层数量生成建筑物楼层标准信息"
	* 
	* @upFloors "地上建筑层数"
	* @downFloors "地下建筑层数"
	*/
	private string function parseFloor( required string upFloors, required string downFloors ) output=false {
		
		var buildFloor = "";
		
		if (isNumeric(arguments.upFloors)) {
			buildFloor = listAppend(buildFloor, numberFormat(trim(arguments.upFloors), "0_"));
		}
		else {
			buildFloor = listAppend(buildFloor, '01');
		}
		
		if (isNumeric(arguments.downFloors)) {
			buildFloor = listAppend(buildFloor, numberFormat(trim(arguments.downFloors), "0_"));
		}
		else {
			buildFloor = listAppend(buildFloor, '00');
		}
		
		return buildFloor;
	}
	
}

</cfscript>